import SysConfig
import datetime
import copy
import re

class CItemAnalysis(object):
	def __init__(self):
		self.allData = {};
		self.worldid = 0;
		for nItemId in SysConfig.list_item_concerned_id:
			self.allData[nItemId] = {};
			itemdata = self.allData[nItemId];
			
			nStartDate = SysConfig.list_concerned_time[0][0];
			nEndDate = SysConfig.list_concerned_time[1][0];
			
			dateStart = datetime.datetime(nStartDate // 10000, nStartDate % 10000 // 100, nStartDate % 10000 % 100);
			dateEnd = datetime.datetime(nEndDate // 10000, nEndDate % 10000 // 100, nEndDate % 10000 % 100);
			
			
			tmpDate = copy.deepcopy(dateStart);
			while(tmpDate <= dateEnd):
				ntmpTime = tmpDate.year * 10000 + tmpDate.month * 100 + tmpDate.day;
				itemdata[ntmpTime] = {};
				itemdata[ntmpTime]['resID'] = nItemId;
				itemdata[ntmpTime]['dateTime'] = ntmpTime;
				itemdata[ntmpTime]['ConsumeUsedNum'] = 0;
				itemdata[ntmpTime]['ConsumeuerGidlist'] = [];
				itemdata[ntmpTime]['PropFlowLostNum'] = 0;
				itemdata[ntmpTime]['PropFlowLostRoleGidList'] = [];
				itemdata[ntmpTime]['PropFlowGetNum'] = 0;
				itemdata[ntmpTime]['PropFlowGetRoleGidList'] = [];
				
				
				tmpDate = tmpDate + datetime.timedelta(1);
			
			self.dateStart = dateStart;
			self.dateEnd = dateEnd;
			
	
	def get_item_data(self, itemID, date):
		if itemID not in SysConfig.list_item_concerned_id:
			return None;
		
		tmpdate = datetime.datetime(date // 10000, date % 10000 // 100, date % 10000 % 100);
		if tmpdate < self.dateStart or tmpdate > self.dateEnd:
			return None;
			
		return self.allData[itemID][date];
	
	def process_keywords_itemConsume(self, line, consumeKey):
		
		#		          *time  *zone  uin   *gid  name  pro  gend lev  typ *ResID  gid  fc  sc  rtye *num
		#ItemConsume\|.+?\|(.+?)\|(.+?)\|.+?\|(.+?)\|.+?\|.+?\|.+?\|.+?\|.+?\|(.+?)\|.+?\|.+?\|.+?\|.+?\|(.+?)
		patt = consumeKey + '\|.+?\|(.+?)\|(.+?)\|.+?\|(.+?)\|.+?\|.+?\|.+?\|.+?\|.+?\|(.+?)\|.+?\|.+?\|.+?\|.+?\|(.+?)';
		m = re.match(patt, line);
		if m is not None and len(m.groups()) == 5:
			strTime = m.groups()[0];
			strZone = m.groups()[1];
			strGid = m.groups()[2];
			nItemID = (int)(m.groups()[3]);
			nNum = (int)(m.groups()[4]);
			
			nTime = (int)(strTime[0:4]) * 10000 + (int)(strTime[5:7])*100 + (int)(strTime[8:10]);
			
			itemData = self.get_item_data(nItemID, nTime);
			if itemData != None:
				itemData['ConsumeUsedNum'] = itemData['ConsumeUsedNum'] + nNum;
				if strGid not in itemData['ConsumeuerGidlist']:
					itemData['ConsumeuerGidlist'].append(strGid);
	
	def process_keywords_itemflow(self, line):
		patt = "%s%s%s%s%s%s%s%s" % \
		("ItemFlow\|.+?\|",
		"(.+?)\|(.+?)\|.+?\|",  #time zone ip
		".+?\|.+?\|(.+?)\|.+?\|.+?\|.+?\|",#uin name gid prof gender level
		".+?\|(.+?)\|.+?\|.+?\|", #Item: gid resid restype isbinded
		".+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|", #chRecvrType iCurRecvrValue bTimeType iLeftTime iBeginTime iDealLine iSkillID
		".+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|.+?\|", 
		".+?\|", #szCreate(for equipsoul)
		"(.+?)\|(.+?)\|.+?\|(.+?)");#num action ispworld GetOrLost
		m = re.match(patt, line);
		if m is not None and len(m.groups()) == 7:
			strTime = m.groups()[0];
			strZone = m.groups()[1];
			strGid = m.groups()[2];
			nItemID = (int)(m.groups()[3]);
			nNum = (int)(m.groups()[4]);
			nAction = (int)(m.groups()[5]);
			nGetOrLost = (int)(m.groups()[6]);
			
			if SysConfig.nPropFlowType != 0 and SysConfig.nPropFlowType != nAction:
				return;
			
			nTime = (int)(strTime[0:4]) * 10000 + (int)(strTime[5:7])*100 + (int)(strTime[8:10]);
			itemData = self.get_item_data(nItemID, nTime);
			if itemData != None:
				if nGetOrLost == 1:
					itemData['PropFlowGetNum'] = itemData['PropFlowGetNum'] + nNum;
					if strGid not in itemData['PropFlowGetRoleGidList']:
						itemData['PropFlowGetRoleGidList'].append(strGid);
				else:
					itemData['PropFlowLostNum'] = itemData['PropFlowLostNum'] + nNum;
					if strGid not in itemData['PropFlowLostRoleGidList']:
						itemData['PropFlowLostRoleGidList'].append(strGid);
					
				
			
			
			
		
	
	def process_lines(self, keywords, line):
		if keywords == 'ItemConsume':
			self.process_keywords_itemConsume(line, keywords);
		elif keywords == 'ImpItemConsume':
			self.process_keywords_itemConsume(line, keywords);
		elif keywords == 'ItemFlow':
			self.process_keywords_itemflow(line);
		else:
			pass;
		
	def output_log(self):
		filename = 'ItemAnalysis_world%d.log.txt' % self.worldid; 
		fp = open(filename, 'w+');
		fp.write('itemid\t dateTime\t ConsumeUsedNum\t ConsumeUsedRoleNum PropFlowLostNum\t PropFlowLostRoleNum\t PropFlowGetNum\t PropFlowGetRoleNum\n');
		for key1 in self.allData.keys():
			for key2 in self.allData[key1].keys():
				data = self.allData[key1][key2];
				strOutput = "%10d\t %10d\t %10d\t %10d %10d\t %10d\t %10d\t %10d\t\n" % \
				(data['resID'],
				data['dateTime'], 
				data['ConsumeUsedNum'], 
				len(data['ConsumeuerGidlist']),
				data['PropFlowLostNum'],
				len(data['PropFlowLostRoleGidList']),
				data['PropFlowGetNum'],
				len(data['PropFlowGetRoleGidList']));
				
				fp.write(strOutput);
			fp.write('-------------------------------------------------------------------------------------------------------------------------\n');
		fp.close();
		
		